package cn.kgc.vue.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.entity.Score;
import cn.kgc.vue.mapper.ScoreMapper;
import cn.kgc.vue.response.ResponseResult;
import cn.kgc.vue.service.ScoreService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author mengshun
* @description 针对表【sys_score】的数据库操作Service实现
* @createDate 2024-10-06 08:39:06
*/
@Service
@Transactional
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements ScoreService{


    @Override
    public ResponseResult scorePage(QueryCondition<Score> queryCondition) {

        Page<Score> scorePage = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();

        Score searchParams = queryCondition.getSearchParams();
        Date examTime = searchParams.getExamTime();
        String clazz = searchParams.getClazz();

        lambda.eq(ObjectUtils.isNotNull(examTime),Score::getExamTime,examTime)
                .eq(StringUtils.isNotBlank(clazz),Score::getClazz,clazz)
                .orderByDesc(Score::getExamTime);

        baseMapper.selectPage(scorePage,lambda);

        return ResponseResult.success().data("total",scorePage.getTotal())
                .data("tableData",scorePage.getRecords());
    }

    @Override
    public ResponseResult importScore(MultipartFile file) {


        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        importParams.setHeadRows(1);

        try {
            InputStream inputStream = file.getInputStream();
            List<Score> scores = ExcelImportUtil.importExcel(inputStream, Score.class, importParams);
            scores.forEach(score ->  baseMapper.insert(score) );
            return  ResponseResult.success();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public void exportScore(Map<String, Object> params, HttpServletResponse response) {
        Object clazz = params.get("clazz");
        Object examTime = params.get("examTime");

        LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();
        lambda.eq(ObjectUtils.isNotNull(clazz),Score::getClazz,clazz)
                .eq(ObjectUtils.isNotNull(examTime),Score::getExamTime,examTime);

        List<Score> scores = baseMapper.selectList(lambda);

        // 导出学生成绩  excel
        ExportParams exportParams = new ExportParams();
        exportParams.setTitle("学生考试成绩表");
        exportParams.setSheetName("成绩表");

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Score.class, scores);
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult scoreDataVisual() {

      List<Score> scores =   baseMapper.scoreDataVisual();
        return ResponseResult.success().data("scoreList",scores);
    }
}




